<?php
$path="C:/xampp/htdocs/00_JO/JOi/";
include_once( $path."DataProvider/DataProvider.php" );
include_once( $path."JO_02_function.php"		);
init();

?>

<?php

	function init(){
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="article_insert"){
			JO_article_insert();
			if($_POST['SO_type']=="fan_corner")
			JO_location("../index.php?SO_unit=07_article&type=fan_corner");
			else
			JO_location("../index.php?SO_unit=07_article");
			
		}
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="article_delete"){
			JO_article_updatedel();
			if($_POST['SO_type']=="fan_corner")
			JO_location("../index.php?SO_unit=07_article&type=fan_corner");
			else
			JO_location("../index.php?SO_unit=07_article");
			
			
		}
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="article_update"){
			
			JO_article_update();
			if($_POST['SO_type']=="fan_corner")
			JO_location("../index.php?SO_unit=07_article&type=fan_corner");
			else
			JO_location("../index.php?SO_unit=07_article");
			
			
			
		}
	}

	/*
	 * ============================================== Get List of Topic ======================================
	 * Author			: Nguyem Minh Tuan
	 * Created Date		: 23/08/2011
	 * Modified Date	: 23/08/2011
	 */

	/*
	 * Get list of topic with name of parent field
	 */
	function JO_article_getList(){
		$s_SQLCommand = "call JO_article_getList();";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_getpagelist($vt,$limit){
		$s_SQLCommand = "call JO_article_getpagelist($vt,$limit);";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_getupload($key){
		$s_SQLCommand = "call JO_article_getload($key);";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_gettopic($parent_id){
		$s_SQLCommand = "call JO_article_gettopic('$parent_id');";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}function JO_article_gettopic1($parent_id){
		$s_SQLCommand = "call JO_article_gettopic1('$parent_id');";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_serchchild($key){
		$s_SQLCommand = "call JO_article_searchchild('$key')";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_updatedel(){
		$key=$_POST['chk_delete'];
		
		foreach($key as $v=>$k)
		{
			//$id=strstr($k,"|");
			//$id=substr($id,1);
			//$sub=strstr($k,"|",true);
			//$result=JO_article_serchchild($sub);
			//$result=$result->num_rows;
			//if($result==0)
			//{
				$s_SQLCommand = "call JO_article_updatedel($k)";
				$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
				
			//}
			//else
			//{
			//	return 0;
			//}
		}
	}
	function test(){
		$s_SQLCommand = "call test();";
		$data=array();
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		if($rs_result!=NULL){
		while($rows=$rs_result->fetch_array()){
			array_push($data,$rows);
		}
		}
		return $data; 
	}
	$result=test();
         $_S="";
	function getchild($arr,$key){									
	for($i=0;$i<count($arr);$i++){
            global  $_S;
			//echo $i;
			//exit;
			if($arr[$i][1]==$key){
				$_S.=getchild($arr,$arr[$i][0]);
				//echo $arr[$i][0]."asdasd";
			}
		}
		return $key.";".$_S; 
		
	}
	function JO_load_article($key,$vt,$limit){
		$result=test();
		$kq=";".getchild($result,$key).";";	
		$s_SQLCommand = "call JO_load_article('$kq',$vt,$limit)";
		
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
							
	/*function JO_article_serchchild($key){
		$s_SQLCommand = "select * form article where `article`.topic in ($key)";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}*/
		
	
	/*
	 * Add a topic
	 */
	 
	 
	  function SO_Upload_Image($SO_file,$pathfile){
			$today = gmdate("YmdHis",time()+(7*3600));
			if ((($_FILES[$SO_file]["type"] == "image/gif")
			|| ($_FILES[$SO_file]["type"] == "image/jpeg")
			|| ($_FILES[$SO_file]["type"] == "image/pjpeg"))
			&& ($_FILES[$SO_file]["size"] < 2000000))//2,000,000:2MB
			{
			  if ($_FILES[$SO_file]["error"] > 0)
			  	return 1;
			  else{	
			    if (file_exists("../".$pathfile."/".$_FILES[$SO_file]["name"]))
			    	return 2;
			    else{
			      move_uploaded_file($_FILES[$SO_file]["tmp_name"],"../".$pathfile."/".$today."_".$_FILES[$SO_file]["name"]);
			      return $today."_".$_FILES[$SO_file]["name"];
			    }
			  }
			}
			else
			  return 3;
		}
	function JO_article_insert(){
		$path="./JO_Upload/";
		if(isset($_FILES['txt_image'])){
		if ($_FILES['txt_image']['name'] != '')//khi nhap lieu, nguoi dung co chon hinh
		{
			if ($_FILES['txt_image']['error'] == " ") //upload ko co loi
			{
				if ($_FILES['txt_image']['size'] < 200000 )
				{
					if(file_exists(	"../JO_Upload/".$_FILES['txt_image']['name'])==false)
					{
						move_uploaded_file($_FILES['txt_image']['tmp_name'], "../JO_Upload/".$_FILES['txt_image']['name']);
					}
				}
			}
		}}
		$topic= $_POST['cb_topic'];
		$title=JO_convertHTMLentities(addcslashes($_POST['txt_title'],"<,>"));
		$description=JO_convertHTMLentities(addcslashes($_POST['ta_description'],"<,>"));
		$content=JO_convertHTMLentities(addcslashes($_POST['ta_content'],"<,>"));
		if(isset($_FILES['txt_image'])){
		$image=JO_convertHTMLentities(addcslashes($_FILES['txt_image']['name'],"<,>"));
		$image=$path.$image;}
		else{
		$image=JO_convertHTMLentities(addcslashes($_POST['txt_image'],"<,>"));
		$image=$image;
		}
		$approval=($_POST['chk_approval'])?1:0;
		$s_SQLCommand = "call JO_article_insert($topic,'$title','$description','$content','$image',$approval);";
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		
	}
	function JO_article_update(){
		$path="./JO_Upload/";
		if ($_FILES['txt_image']['name'] != '')//khi nhap lieu, nguoi dung co chon hinh
		{
			if ($_FILES['txt_image']['error'] == " ") //upload ko co loi
			{
				if ($_FILES['txt_image']['size'] < 200000 )
				{
					if(file_exists(	"../JO_Upload/".$_FILES['txt_image']['name'])==false)
					{
						move_uploaded_file($_FILES['txt_image']['tmp_name'], "../JO_Upload/".$_FILES['txt_image']['name']);
					}
				}
			}
		}
		$key=$_POST['SO_chude'];
		$topic=$_POST['cb_topic'];
		$title=JO_convertHTMLentities(addcslashes($_POST['txt_title'],"<,>"));
		$description=JO_convertHTMLentities(addcslashes($_POST['ta_description'],"<,>"));
		$content=JO_convertHTMLentities(addcslashes($_POST['ta_content'],"<,>"));
		if(isset($_FILES['txt_image'])){
		$image=JO_convertHTMLentities(addcslashes($_FILES['txt_image']['name'],"<,>"));
		$image=$path.$image;}
		else{
		$image=JO_convertHTMLentities(addcslashes($_POST['txt_image'],"<,>"));
		$image=$image;
		}
		$approval=($_POST['chk_approval'])?1:0;
		$s_SQLCommand = "call JO_article_update($topic,'$title','$description','$content','$image',$approval,$key)";
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		//JO_location();
	}
	
?>